package com.all.play.mysvnbanchone.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by 聪明一只哈 on 2018/5/21.
 * Time: 2018/5/21  14:04
 */

public class MyNewGridItemDecration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int[] attr = new int[]{android.R.attr.listDivider};

    public MyNewGridItemDecration(Context context) {
        TypedArray typedArray =
                context.obtainStyledAttributes(attr);

        mDivider = typedArray.getDrawable(0);
        typedArray.recycle();

    }



    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //左上右下,代表4个方向上,分割线的偏移量-->分割线与条目的距离
        //一个分割线是一个矩形
        //分割线在条目的四周,四个方向上的距离
//        int childAdapterPosition = parent.getChildAdapterPosition(view);
//        View childAt = parent.getChildAt(childAdapterPosition);
//        int top=childAt.getTop();
//        int left=childAt.getRight();
        int right = mDivider.getIntrinsicWidth();
        int bottom = mDivider.getIntrinsicHeight();

        int position = parent.getChildAdapterPosition(view);

//        if(isLastColum(position,parent)){
//            outRect.set(0,0,0,bottom);
//        }else if(isLastRaw(position,parent)){
//            outRect.set(0,0,right,0);
//        }else{
//            outRect.set(0, 0, right, bottom);
//        }

        if (isLastColum(position,parent)){
            right=0;
        }
        if (isLastRaw(position,parent)){
            bottom=0;
        }

        outRect.set(0, 0, right, bottom);
    }

    private boolean isLastRaw(int position, RecyclerView parent) {
        GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
        if (manager instanceof GridLayoutManager){
            int spanCount = manager.getSpanCount();
            int childCount = parent.getAdapter().getItemCount();
            //共多少个 对  共多少列  求余 =最后一个行view的个数
            int lastRowCount = childCount % spanCount;

            if (lastRowCount == 0 || lastRowCount < spanCount) {
                return true;
            }
        }


        return false;
    }

    private boolean isLastColum(int position, RecyclerView parent) {

        GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();
        if (manager instanceof GridLayoutManager){
            int spanCount = manager.getSpanCount();
            if ((position+1)%spanCount==0){
                return true;
            }
        }

        return false;
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c, parent);
        drawHorizontal(c, parent);
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        //画水平线
        int childCount = parent.getChildCount();
        for (int i=0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left=child.getLeft()-params.leftMargin;
            int right=child.getRight()+params.rightMargin+mDivider.getIntrinsicWidth();
            int top=child.getBottom()+params.bottomMargin;
            int bottom=top+mDivider.getIntrinsicHeight();



            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        //画垂直线,-->画矩形,矩形包含着条目
        int childCount = parent.getChildCount();
        for (int i=0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left=child.getRight()+params.rightMargin;
            int right=left+mDivider.getIntrinsicWidth();
            int top=child.getTop()-params.topMargin;
            int bottom=child.getBottom()+params.bottomMargin;



            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }
}
